﻿using CommonLib.Json;
using Microsoft.AspNetCore.Mvc.Filters;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WXQ.InOutPutEntites.Output;
using Microsoft.AspNetCore.Mvc;

namespace WebApi.Common.Aop
{
    public class ValidateInputParaAttribute:BaseActionFilterAsync
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override async Task OnActionExecuting(ActionExecutingContext context)
        {
            if (!context.ModelState.IsValid)
            {
           
                ResponseResult result = new ResponseResult();
                result.Code = WXQ.Enties.CommonObj.ResponseResultMessageDefine.ParaError;
                result.Errors = context.ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage).ToList();
                context.Result = JsonContent( result.ToJson());
            }

            await Task.CompletedTask;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public Microsoft.AspNetCore.Mvc.ContentResult JsonContent(string json)
        {
            return new ContentResult { Content = json, StatusCode = 200, ContentType = "application/json; charset=utf-8" };
        }
    }
}
